﻿using DotNetOpenAuth.Messaging;
using Inovout.Runtime;
using System;
using System.Collections.Generic;

namespace Inovout.Web.Security
{
    public class BaiduOAuthClient : BaseOAuth2Client
    {
        private const string BaiduAuthorizationEndpoint = "https://openapi.baidu.com/oauth/2.0/authorize";
        private const string BaiduTokenEndpoint = "https://openapi.baidu.com/oauth/2.0/token";
        private const string BaiduUserDataServiceEndpoint = "https://openapi.baidu.com/rest/2.0/passport/users/getLoggedInUser";

        public static readonly string OAuth2ClientProviderName = "baidu";
        public static readonly Dictionary<string, string> userDataMapping = new Dictionary<string, string>{
            {"id", "uid"},{"username", "uname"},{"userloginimg", "portrait"}
        };

        public BaiduOAuthClient(string providerName)
            : this(providerName,
            ComponentRegistry.Resolve<IApplication>().Settings["BaiduClientId"].ToString(),
                   ComponentRegistry.Resolve<IApplication>().Settings["BaiduClientSecret"].ToString(),
            userDataMapping,
            BaiduAuthorizationEndpoint,
            BaiduTokenEndpoint,
            BaiduUserDataServiceEndpoint)
        {
        }
        public BaiduOAuthClient()
            : this(OAuth2ClientProviderName,
                ComponentRegistry.Resolve<IApplication>().Settings["BaiduClientId"].ToString(),
                ComponentRegistry.Resolve<IApplication>().Settings["BaiduClientSecret"].ToString(),
            userDataMapping,
            BaiduAuthorizationEndpoint,
            BaiduTokenEndpoint,
            BaiduUserDataServiceEndpoint
                )
        {

        }

        public BaiduOAuthClient(string providerName, string clientId, string clientSecret,
                                    Dictionary<string, string> userDataMapping,
                                  string authorizeEndpoint, string tokenServiceEndpoint, string userDataServiceEndpoint,
                                   string scope = null)
            : base(providerName, clientId, clientSecret, userDataMapping, authorizeEndpoint, tokenServiceEndpoint, userDataServiceEndpoint, false, scope)
        {

        }
        protected override void AppendServiceLoginUrl(UriBuilder builder)
        {
            builder.AppendQueryArgument("force_login", "1");
        }
    }
}
